package com.yh.homestay.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.dao.SysRoleMapper;
import com.yh.homestay.dao.SysStaffRoleMapper;
import com.yh.homestay.pojo.SysRole;
import com.yh.homestay.pojo.SysStaffRole;
import com.yh.homestay.service.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    private Log logger = LogFactory.getLog(SysRoleServiceImpl.class);

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysStaffRoleMapper sysStaffRoleMapper;

    /**
     * @param roleName
     * @param page
     * @param limit
     * @Date: 2020/1/13 10:03
     * @Description: 查询出所有角色信息
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult showRoles(String roleName, Integer page, Integer limit) {
        try {
            PageHelper.startPage(page, limit);
            List<SysRole> sysRoles;
            if (StringUtils.isEmpty(roleName)) {
                sysRoles = sysRoleMapper.selectAll();
            } else {
                Example example = new Example(SysRole.class);
                example.createCriteria().andLike("roleName", "%" + roleName + "%");
                sysRoles = sysRoleMapper.selectByExample(example);
            }
            PageInfo info = new PageInfo(sysRoles);
            return new HomeStayResult(0, "ok", (int) info.getTotal(), info.getList());
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param sysRole
     * @Date: 2020/1/13 10:04
     * @Description: TODO
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult updateRole(SysRole sysRole) {
        try {
            sysRoleMapper.updateByPrimaryKeySelective(sysRole);
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param id
     * @Date: 2020/1/13 10:05
     * @Description: TODO
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult deleteRole(Integer id) {
        try {
            sysRoleMapper.deleteByPrimaryKey(id);
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param id
     * @Date: 2020/1/13 10:06
     * @Description: TODO
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult showOneRole(Integer id) {
        try {
            SysRole sysRole = sysRoleMapper.selectByPrimaryKey(id);
            return HomeStayResult.ok(200,sysRole);
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param ids
     * @Date: 2020/1/13 10:07
     * @Description: TODO
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult delectBatchRole(List<Integer> ids) {
        try {
            for (Integer id : ids) {
                sysRoleMapper.deleteByPrimaryKey(id);
            }
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param sysRole
     * @Date: 2020/1/13 10:08
     * @Description: TODO
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult insertRole(SysRole sysRole) {
        try {
            sysRoleMapper.insertSelective(sysRole);
            return HomeStayResult.ok();
        }catch (Exception e){
            return HomeStayResult.build(500,"error");
        }
    }

    @Override
    public List<String> getRoleName(Integer staffId) {
        Example example = new Example(SysStaffRole.class);
        example.createCriteria().andEqualTo("staffId", staffId);
        List<SysStaffRole> staffRoles = sysStaffRoleMapper.selectByExample(example);
        List<String> roles = new ArrayList<String>();
        for (SysStaffRole staffRole : staffRoles) {
            SysRole sysRole = sysRoleMapper.selectByPrimaryKey(staffRole.getRoleId());
            roles.add(sysRole.getRoleName());
        }
        return roles;
    }
}
